<?php

/**
 * 影院管理 -> 影院列表
 */
class Action
{
    private $per_page = 50;

    public function __construct() {

    }

    /**
     * 显示列表
     */
    public function index() {
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', -1);
        $progress = Request::getParam('progress', '-1');
        $search_filter = Request::getParam('search_filter', array());
        $is_delete = Request::getParam('is_delete', 1);
        $type = Request::getParam('type', -1);
        $movie_type = Request::getParam('movie_type', -1);
        $cinema_type = Request::getParam('cinema_type', -1);
        $zzb_type = Request::getParam('zzb_type', -1);
        $no_long_lati = Request::getParam('no_long_lati', 0);

        // 拼装搜索条件
        $filter = array();

        //获取当前用户可看的区域
        $role_id = user_helper::get_admin_role_id();
        if (!user_helper::is_all_can_show($role_id)) {
            $filter['id'] = store_helper::get_store_ids_by_admin_user();
            if (!$filter['id']) {
                $filter['id'] = -1;
            }
        }

        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }

        if ($type != -1) {
            $filter['type'] = [$type];
        } else {
            $filter['type'] = '100'; //'100' => '电影院'
        }

        if ($cinema_type > 0) {
            $filter['zzb_id'] = _model('cinema_zzb')->getFields('zzb_code', array('circuit' => store_config::$cinema_type[$cinema_type]));
        }

        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%' . $search_filter['title'] . '%';
        }

        if (isset($search_filter['dealer_name']) && $search_filter['dealer_name']) {
            $dealer_ids = user_helper::get_user_ids_by_realname($search_filter['dealer_name']);
            if ($dealer_ids) {
                $filter['agency_id'] = $dealer_ids;
            }
        }

        if (isset($search_filter['realname']) && $search_filter['realname']) {
            $store_user_ids = user_helper::get_user_ids_by_realname($search_filter['realname']);
            if ($store_user_ids) {
                $filter['user_id'] = $store_user_ids;
            }
        }

        if (isset($search_filter['mobile']) && $search_filter['mobile']) {
            $store_user_id_list = user_helper::get_user_ids_by_mobile($search_filter['mobile']);
            if ($store_user_id_list) {
                if (isset($filter['user_id'])) {
                    $filter['user_id'] = array_intersect($filter['user_id'], $store_user_id_list);
                } else {
                    $filter['user_id'] = $store_user_id_list;
                }
            }
        }

        if (isset($search_filter['district_id']) && $search_filter['district_id']) {
            $filter['district_id'] = $search_filter['district_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['district_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['city_id']) && $search_filter['city_id']) {
            $filter['city_id'] = $search_filter['city_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['city_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['province_id']) && $search_filter['province_id']) {
            $filter['province_id'] = $search_filter['province_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['province_id']);
            Response::assign('area', $area);
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }

        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }

        if ('-1' != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >= '] = 0;
        }

        $filter['is_delete'] = 1;

        // 没有经纬度
        if ($no_long_lati) {
            $filter['(`longitude`'] = 0;
            $filter['OR `latitude`'] = 0;
            $filter[') and `id` > '] = 0;
        }

        if ($zzb_type == 1) {
            $filter['zzb_id'] = 0;
        } elseif ($zzb_type == 2) {
            $filter['zzb_id >'] = 0;
        }

        // 传-1的时候查看所有的信息
        if ($is_delete != -1) {
            $filter['is_delete'] = $is_delete;
        }

        $filter['is_enable'] = 1;

        $filter['`agency_id` !='] = 0;

        $store_list = get_data_list('store', enable_filter($filter), ' order BY `id` DESC ', $page, $this->per_page);
        if ($store_list) {
            $type_arr = store_config::$type_arr;

            foreach ($store_list as $k => $v) {
                $user_info = _uri('user', @$v['owner_id']);
                if ($user_info) {
                    $store_list[$k]['user_name'] = $user_info['user_name'];
                    $store_list[$k]['mobile'] = $user_info['mobile'];
                }
                if (isset($type_arr[$v['type']])) {
                    $store_list[$k]['type_name'] = $type_arr[$v['type']];
                }
                if ($v['agency_id']) {
                    $dealer_info = _uri('user', $v['agency_id']);
                    if ($dealer_info) {
                        $store_list[$k]['dealer_name'] = $dealer_info['realname'];
                        $store_list[$k]['dealer_mobile'] = $dealer_info['mobile'];
                    }
                }
                if ($v['cinema_assistant_id']) {
                    $dealer_info = _uri('user', $v['cinema_assistant_id']);
                    if ($dealer_info) {
                        $store_list[$k]['cinema_assistant_name'] = $dealer_info['realname'];
                        $store_list[$k]['cinema_assistant_mobile'] = $dealer_info['mobile'];
                    }
                }
                if ($v['cinema_manager_id']) {
                    $dealer_info = _uri('user', $v['cinema_manager_id']);
                    if ($dealer_info) {
                        $store_list[$k]['cinema_manager_name'] = $dealer_info['realname'];
                        $store_list[$k]['cinema_manager_mobile'] = $dealer_info['mobile'];
                    }
                }
                //                if ($v['cinema_investment_manager_id']) {
                //                    $dealer_info = _uri('user', $v['cinema_investment_manager_id']);
                //                    if ($dealer_info) {
                //                        $store_list[$k]['cinema_investment_manager_name'] = $dealer_info['realname'];
                //                        $store_list[$k]['cinema_investment_manager_mobile'] = $dealer_info['mobile'];
                //                    }
                //                }
                $store_list[$k]['cinema_name'] = $v['title'];
                //                $cinema_info = _uri('cinema_zzb', array('zzb_code' => $v['zzb_id']));
                //                if ($cinema_info) {
                //                    $store_list[$k]['cinema_name'] = $cinema_info['cinema_name'] ? $cinema_info['cinema_name']
                //                        : $v['title'];
                //                }
            }
        }

        Response::assign('store_list', $store_list);
        Response::assign('status', $status);
        Response::assign('movie_type', $movie_type);
        Response::assign('is_delete', $is_delete);
        Response::assign('type', $type);
        Response::assign('cinema_type', $cinema_type);
        Response::assign('zzb_type', $zzb_type);
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/cinema_list.html');
    }

    /**
     * 修改状态
     */
    public function change_status() {
        $id = Request::getParam('id', 0);
        $status = Request::getParam('status', -1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $info = _uri('store', $id);
        if ($status == $info['status']) {
            return '已经执行过此操作，不用重复提交';
        }
        _model('store')->update($id, array('status' => $status));

        return 'ok';
    }

    /**
     * 恢复运营
     */
    public function recover_store() {
        $id = Request::getParam('id', '0');
        $extra = Request::getParam('extra', '');
        $store_type = Request::getParam('store_type', -1);

        $extra = substr($extra, 0, 100);

        if (!$id) {
            return '请选择要更改的内容';
        }

        $info = _uri('store', $id);
        //         if (store_config::STATUS_NORMAL == $info['status']) {
        //             return '已经执行过此操作，不用重复提交';
        //         }

        _model('store')->update($id, array('status' => store_config::STATUS_NORMAL, 'extra' => $extra));

        if ($store_type == -1) {
            Response::redirect(AnUrl('store/admin'));
        } else {
            Response::redirect(AnUrl('store/admin/movie_store'));
        }

    }


    /**
     * 编辑影院信息
     */
    public function edit() {
        $id = Request::getParam('id', 0);
        $store_type = Request::getParam('store_type', -1);

        if (!$id) {
            return '请选择门店';
        }

        $store_info = store_helper::get_store($id);
        if (!$store_info) {
            return '此门店不存在';
        }
        $role_id = user_helper::get_admin_role_id();

        $user_info = user_helper::get_user_info($store_info['user_id']);
        if (!$user_info) {
            return '掌柜不存在';
        }

        $store_info['user_name'] = $user_info['realname'] ? $user_info['realname'] : $user_info['nickname'];
        $store_info['mobile'] = $user_info['mobile'];

        // 获取合伙人列表
        $agency_list = user_helper::get_all_dealers_info();

        // 市场人员列表
        $marketing_list = dealers_helper::get_marketing_list_by_agency_id($store_info['agency_id']);
        array_unshift($marketing_list, user_helper::get_user_info($store_info['agency_id']));

        // 获取影院销售列表
        $cinema_assistant_list = user_helper::get_role_user_ids(user_config::CINEMA_ASSISTANT, 'ORDER BY `id` DESC');
        $cinema_assistant_info = array();
        if ($cinema_assistant_list) {
            $cinema_assistant_info = _model('user')->getList(array('id' => $cinema_assistant_list));
        }

        // 获取院线管理员列表
        $cinema_manager_list = user_helper::get_role_user_ids(user_config::CINEMA_MANAGER, 'ORDER BY `id` DESC');
        $cinema_manager_info = array();
        if ($cinema_manager_list) {
            $cinema_manager_info = _model('user')->getList(array('id' => $cinema_manager_list));
        }

        if ($store_info['district_id']) {
            $area = _widget('area')->get_area_info_by_area_code($store_info['district_id']);
        } elseif ($store_info['city_id']) {
            $area = _widget('area')->get_area_info_by_area_code($store_info['city_id']);
        } elseif ($store_info['province_id']) {
            $area = _widget('area')->get_area_info_by_area_code($store_info['province_id']);
        } else {
            $area = array();
        }

        // 门店图片
        $store_info['cover'] = _image($store_info['cover']);

        //合同编号
        $contract = _model('store_contract')->read(array('store_id' => $id));
        if ($contract) {
            $store_info['contract'] = $contract['code'];
        }

        Response::assign('role_id', $role_id);
        Response::assign('id', $id);
        Response::assign('area', $area);
        Response::assign('store_type', $store_type);
        Response::assign('store_info', $store_info);
        Response::assign('marketing_list', $marketing_list);
        Response::assign('agency_list', $agency_list);
        Response::assign('cinema_manager_info', $cinema_manager_info);
        //        Response::assign('cinema_investment_manager_info', $cinema_investment_manager_info);
        Response::assign('cinema_assistant_info', $cinema_assistant_info);
        Response::display('admin/edit.html');
    }

    /**
     * 更新门店信息
     */
    public function update() {
        $info = Request::getParam('info', array());
        $store_id = $info['id'];
        $contract_id = trim($info['contract']);
        unset($info['contract']);

        if ($info['marketing_id'] == 0) {
            $info['marketing_id'] = $info['agency_id'];
        }

        $k = 'cover';
        if (isset($_FILES[$k]) && $_FILES[$k]['tmp_name']) {
            $image_info = upload_file($_FILES[$k], false);
            if ($image_info) {
                $info[$k] = isset($image_info['file_path']) ? $image_info['file_path'] : $image_info;
            }
        }

        $affected = _model('store')->update($store_id, $info);

        //更新合同
        if ($contract_id) {
            $contract = _model('store_contract')->read(array('code' => $contract_id));
            if ($contract) {
                _model('store_contract')->update(array('code' => $contract_id), array('store_id' => $store_id));
            } else {
                _model('store_contract')->create(array('code' => $contract_id, 'store_id' => $store_id));
            }
        }

        if ($affected) {
            Response::redirect(AnUrl('cinema/admin/cinema_manage/show?id=' . $store_id));
        } else {
            Response::redirect(AnUrl('cinema/admin/cinema_manage/show?id=' . $store_id));
        }

    }

    /**
     *
     * 影院详情页
     */
    public function show() {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return '请选择门店!';
        }

        $info = _uri('store', $id);

        $zzb_info = array();
        $store_device_list = array();
        if ($info) {
            // 获取分店店长,总店店长和签约人员姓名
            $user_info = _uri('user', $info['owner_id']);
            $info['owner_name'] = $user_info['realname'] ? $user_info['realname'] : $user_info['nickname'];
            $info['mobile'] = $user_info['mobile'];

            $info['agency_info'] = _uri('user', $info['agency_id']);
            $info['cinema_assistant_info'] = _uri('user', $info['cinema_assistant_id']);
            $info['cinema_manager_info'] = _uri('user', $info['cinema_manager_id']);
            $info['cinema_investment_manager_info'] = _uri('user', $info['cinema_investment_manager_id']);

            $info['level'] = score_record_helper::get_user_score_level($info['score']) . '级';

            $info['store_type'] = store_config::$type_arr[$info['type']];

            foreach (device_config::$TYPES as $k => $v) {
                $list = device_helper::get_store_typed_device_by_store_id($id, $v['id']);
                if ($list) {
                    $store_device_list[$k]['res_name'] = $v['id'];
                    $store_device_list[$k]['res_name_str'] = $v['name'];
                    $store_device_list[$k]['device_list'] = $list;
                }
            }

            $info['store_device_list'] = $store_device_list;

            //专资办数据
            $zzb_info = _uri('cinema_zzb', array('zzb_code' => $info['zzb_id']));
        }

        //获取影院所有订单
        $order_count = _model('order')->getTotal(array('store_id' => $id, 'status' => 1));
        $pager = new Pager($this->per_page);
        if ($pager->generate($order_count)) {
            Response::assign('pager', $pager);
        }
        $order_by = ' ORDER BY `id` DESC ';
        $order_list = _model('order')->getList(array('store_id' => $id, 'status' => 1), $order_by);

        $all_order_money = 0.0;
        $recent_half_year_list = $this->recent_half_year();
        foreach ($order_list as $k => $v) {
            $all_order_money = $all_order_money + (float)$v['price'];
            //获取半年期间影院数据
            foreach ($recent_half_year_list as $ky => $kv) {
                if (strtotime($v['add_time']) >= strtotime($kv[0]) && strtotime($v['add_time']) <= strtotime($kv[1])) {

                    if (isset($kv[2])) {
                        $recent_half_year_list[$ky][2] = $kv[2] + (float)$v['price'];
                    } else {
                        $recent_half_year_list[$ky][2] = (float)$v['price'];
                    }
                }
            }
        }

        $recent_half_year_date_list = array();
        $recent_half_year_money_list = array();
        foreach ($recent_half_year_list as $k => $v) {
            $recent_half_year_date_list[] = '"' . $k . '"';
            $recent_half_year_money_list[] = isset($v[2]) ? $v[2] : 0;
        }

        $order_list = order_helper::decorator_order_model($order_list);

        Response::assign('count', $order_count);
        Response::assign('id', $id);
        Response::assign('store_info', $info);
        Response::assign('store_device_list', $store_device_list);
        Response::assign('zzb_info', $zzb_info);
        Response::assign('all_order_money', $all_order_money);
        Response::assign('order_list', $order_list);
        Response::assign('recent_half_year_money_list', $recent_half_year_money_list);
        Response::assign('recent_half_year_date_list', $recent_half_year_date_list);

        Response::display('admin/cinema_details.html');
    }


    /**
     *
     * 获取门店设备绑定记录
     */
    public function get_store_device_record() {
        $id = Request::getParam('id', 0);
        $is_delete = Request::getParam('is_delete', 1);
        $store_type = Request::getParam('store_type', -1);

        if (!$id) {
            return false;
        }

        $store_info = _uri('store', $id);

        $record_list = array();
        $record_list = store_helper::get_store_device_record($id);

        Response::assign('store_type', $store_type);
        Response::assign('is_delete', $is_delete);
        Response::assign('store_info', $store_info);
        Response::assign('record_list', $record_list);
        Response::display('admin/store_device_record.html');

    }

    /**
     * 获取门店设备信息
     */
    public function get_store_device_list() {
        $id = Request::getParam('id', 0);
        $is_delete = Request::getParam('is_delete', 1);
        $store_type = Request::getParam('store_type', -1);

        if (!$id) {
            return false;
        }

        $store_info = _uri('store', $id);

        $record_list = device_helper::get_store_typed_device_by_store_id($id);

        Response::assign('store_type', $store_type);
        Response::assign('is_delete', $is_delete);
        Response::assign('store_info', $store_info);
        Response::assign('record_list', $record_list);
        Response::display('admin/store_device_list.html');

    }

    /**
     * 删除门店
     */
    public function delete_store() {
        $id = Request::getParam('id');
        if (!$id) {
            return '请选择要删除的内容';
        }
        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k => $v) {
            if (!$v) {
                continue;
            }

            $store_info = _uri('store', (int)$v);
            if (!$store_info) {
                continue;
            }

            if (1 == $store_info['is_delete']) {
                _model('store')->update($v, array('is_delete' => 0));
            }
        }

        return 'ok';

    }

    /**
     * 取消删除
     */
    public function undelete_store() {
        $id = Request::getParam('id');
        if (!$id) {
            return '请选择要删除的内容';
        }
        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k => $v) {
            if (!$v) {
                continue;
            }

            $store_info = _uri('store', (int)$v);
            if (!$store_info) {
                continue;
            }

            if (0 == $store_info['is_delete']) {
                _model('store')->update($v, array('is_delete' => 1));
            }
        }

        return 'ok';
    }

    //最近半年
    private function recent_half_year() {
        $now = date('Y-m-d');
        $month_list = array();
        for ($i = 5; $i >= 0; $i--) {
            $oldday = date('Y-m-d', strtotime("$now - $i month"));
            $oldmonth = date('Y-m', strtotime("$now - $i month"));
            $month_list[$oldmonth] = $this->getTheMonth($oldday);
        }
        return $month_list;
    }

    private function getTheMonth($date) {//获取指定日期所在月的第一天和最后一天
        $firstday = date("Y-m-01 00:00:00", strtotime($date));
        $lastday = date("Y-m-d 23:59:59", strtotime("$firstday +1 month -1 day"));
        return array($firstday, $lastday);
    }


    /**
     * 三维数组转换
     * @return array
     */
    private function exchangeArr($arr) {
        if (!is_array($arr)) {
            return;
        }
        foreach ($arr as $key => $value) {
            foreach ($value as $k => $v) {
                $new_arr[$k][$key] = $v;
            }
        }
        return $new_arr;
    }

}
